﻿using Model;
using System.Net;
using System.Net.Mail;
using System.Text;

namespace Common
{
    public enum EmailServerType
    {
        /// <summary>
        /// 163邮件服务器
        /// </summary>
        SERVER_163_COM,
        /// <summary>
        /// qq邮件服务器
        /// </summary>
        SERVER_QQ_COM,
        /// <summary>
        /// outlook邮件服务器
        /// </summary>
        SERVER_Outlook_COM,
    }
    /// <summary>
    /// 发送邮件DTO
    /// </summary>
    public class SendEmailDTO
    {
        /// <summary>
        /// 发件人
        /// </summary>
        public string MailFrom { get; set; }
        /// <summary>
        /// 收件人
        /// </summary>
        public string MailTo { get; set; }
        /// <summary>
        /// 标题
        /// </summary>
        public string Title { get; set; }
        /// <summary>
        /// 内容
        /// </summary>
        public string Content { get; set; }
        /// <summary>
        /// 发件人账号
        /// </summary>
        public string MailFromAccount { get; set; }
        /// <summary>
        /// 密码
        /// </summary>
        public string MailFromPassword { get; set; }
        /// <summary>
        /// 服务器类型
        /// </summary>
        public EmailServerType ServerType { get; set; }
    }
    /// <summary>
    /// 邮件帮助
    /// </summary>
    public class EmailHelper
    {
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public static async Task<ResponseMessage> SendEmail(SendEmailDTO param)
        {
            try
            {
                //发送电子邮件的实例
                var client = new SmtpClient();
                //设置电子邮件的服务器地址
                switch (param.ServerType)
                {
                    case EmailServerType.SERVER_QQ_COM:
                        client.Port = 587;
                        client.Host = "smtp.qq.com";
                        break;
                    case EmailServerType.SERVER_Outlook_COM:
                        client.Host = "smtp-mail.outlook.com";
                        break;
                    case EmailServerType.SERVER_163_COM:
                        client.Port = 25;
                        client.Host = "smtp.163.com";
                        break;
                    default:
                        throw new Exception("暂不支持该服务器类型");
                }
                #region 邮件服务器配置
                //设置是否使用SSL加密
                client.EnableSsl = true;
                //设置发件人的邮箱地址和密码
                client.Credentials = new NetworkCredential(param.MailFromAccount, param.MailFromPassword);
                //实例化一个邮件类
                var message = new MailMessage
                {
                    //设置邮件的发件人
                    From = new MailAddress(param.MailFrom),
                    //设置邮件的编码
                    BodyEncoding = Encoding.UTF8,
                    //设置邮件的优先级
                    Priority = MailPriority.High,
                    //设置邮件的标题
                    Subject = param.Title,
                    //设置邮件的内容
                    Body = param.Content
                };
                //添加邮件的收件人
                message.To.Add(param.MailTo);
                #endregion
                //发送邮件
                await client.SendMailAsync(message);
                return ResponseMessage.CreateSuccess("发送成功");
            }
            catch
            {
                return ResponseMessage.CreateError("发送失败");
            }
        }
        /// <summary>
        /// 生成验证码
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string GenerateCode(int length= 4)
        {
            string str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            var sb = new StringBuilder();
            var random = new Random();
            for (int i = 0; i < length; i++)
            {
                int index = random.Next(0, str.Length);
                sb.Append(str[index]);
            }
            return sb.ToString();
        }
    }
}
